

import axios from "axios";
import { createDiscreteApi } from "naive-ui";


const { message } = createDiscreteApi(["message"]);
const request = axios.create({
  baseURL: "http://localhost:3001",
  timeout: 1000 * 10,
});

request.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);


request.interceptors.response.use(
  (response) => {
    const { data } = response;
    if (data.code === "200") {
      return data;
    } else {
      message.error(data.message || "网络异常，请稍后重试！");
      throw new Error(data.message || "网络异常，请稍后重试！");
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
